package commonadapter.demo;

import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ListView;

import java.util.ArrayList;
import java.util.List;

import commonadapter.pojo.ListData;
import main.java.wangxinlei.commonadapter.CommonAdapter;
import main.java.wangxinlei.commonadapter.ViewHolder;

public class MainActivity extends ActionBarActivity {


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initUI();
    }

    private void initUI() {
        ListView listView = (ListView) findViewById(R.id.listview);
        List<ListData> listDatas = getData();
        //        NomalAdapter adapter = new NomalAdapter(listDatas, this);

        listView.setAdapter(new CommonAdapter<ListData>(MainActivity.this,
                listDatas, R.layout.view_list_item) {
            @Override
            public void initView(ViewHolder holder, ListData listData) {
                holder.setText(R.id.tv_title, listData.getTitle()).
                        setText(R.id.tv_content, listData.getContent());
            }
        });
    }

    private List<ListData> getData() {
        List<ListData> listDatas = new ArrayList<ListData>();
        ListData data = null;

        for (int i = 0; i < 10; i++){
            data = new ListData();
            data.setTitle("测试数据".concat(String.valueOf(i)));
            data.setContent("jksoifnksdfsdfaegdagdsfewf");
            listDatas.add(data);
        }

        return listDatas;
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

}
